package com.hatricks.is.charset;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import org.mozilla.universalchardet.UniversalDetector;

import java.io.File;
import java.nio.charset.Charset;

/**
 * @packageName: com.hatricks.is.charset
 * @className: CharsetEncodingTest
 * @author: yanyuhui y568821795@outlook.com
 * @date: 2025-01-09 09:47
 * @description:
 */
public class CharsetEncodingTest {
    public static void main(String[] args) {
        File file = FileUtil.file("C:\\work_space\\java_work_space\\public_utils\\infrastructure-s\\src\\main\\java\\com\\hatricks\\is\\ss.json");
        // 使用 juniversalchardet 检测编码
        UniversalDetector detector = new UniversalDetector(null);
        byte[] fileBytes = FileUtil.readBytes(file);
        detector.handleData(fileBytes, 0, fileBytes.length);
        detector.dataEnd();
        String charset = detector.getDetectedCharset();
        detector.reset();
        System.out.println("检测到的编码:====================== " + charset);
        System.out.println(IoUtil.read(IoUtil.toStream(file), Charset.forName(charset)));
    }
}
